﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DacSanMienTayOBJ
{
    public class AdvertisingBusiness
    {
        public static bool InsertAdvertising(Advertising adv, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "insert into advertising(AdvPositionID,AdvName,Logo,Link,Note,CreateDate,ExpireDate,Priority,IsVisible) values(@advPosition,@advName,@logo,@link,@note,@createDate,@expireDate,@priority,@isVisible)";
                SqlParameter[] pa = new SqlParameter[9];
                pa[0] = new SqlParameter("@advPosition", adv.AdvPositionID);
                pa[1] = new SqlParameter("@advName", adv.AdvName);
                pa[2] = new SqlParameter("@logo", adv.Logo);
                pa[3] = new SqlParameter("@link", adv.Link);
                pa[4] = new SqlParameter("@note", adv.Note);
                pa[5] = new SqlParameter("@createDate", adv.CreateDate);
                pa[6] = new SqlParameter("@expireDate", adv.ExpireDate);
                pa[7] = new SqlParameter("@priority", adv.Priority);
                pa[8] = new SqlParameter("@isVisible", adv.IsVisible);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool UpdateAdvertising(int advID,string advName,int advPosition,string logo,string link,string note, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update advertising set AdvPositionID=@advPosition,AdvName=@advName,Logo=@logo,Link=@link,Note=@note where AdvertisingID=@advID";
                SqlParameter[] pa = new SqlParameter[6];
                pa[0] = new SqlParameter("@advPosition", advPosition);
                pa[1] = new SqlParameter("@advName", advName);
                pa[2] = new SqlParameter("@logo",logo);
                pa[3] = new SqlParameter("@link", link);
                pa[4] = new SqlParameter("@note", note);
                pa[5] = new SqlParameter("@advID", advID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static bool LockAndUnlockAdvertising(int advID,int visible, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                bool rs = false;
                string sql = "update advertising set IsVisible=@isVisible where AdvertisingID=@advID";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@isVisible", visible);
                pa[1] = new SqlParameter("@advID", advID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return false;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static Advertising GetAdvertisingDetail(int advID, ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                Advertising adv = null;
                string sql = "select *from advertising where AdvertisingID=@advID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@advID", advID);
                SqlCommand command = helper.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    adv = new Advertising();
                    adv.AdvertisingID = int.Parse(reader["AdvertisingID"].ToString());
                    adv.AdvName = reader["AdvName"].ToString();
                    adv.AdvPositionID = int.Parse(reader["AdvPositionID"].ToString());
                    try
                    {
                        adv.Link = reader["Link"].ToString();
                    }
                    catch
                    {
                        adv.Link = "";
                    }
                    try
                    {
                        adv.Logo = reader["Logo"].ToString();
                    }
                    catch
                    {
                        adv.Logo = "";
                    }
                    try
                    {
                        adv.Note = reader["Note"].ToString();
                    }
                    catch
                    {
                        adv.Note = "";
                    }
                    adv.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
                    adv.ExpireDate = DateTime.Parse(reader["ExpireDate"].ToString());
                    adv.Priority = int.Parse(reader["Priority"].ToString());
                    adv.IsVisible = int.Parse(reader["IsVisible"].ToString());
                }
                return adv;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Advertising> GetListAdvertisingAllAdmin(ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<Advertising> list = new List<Advertising>();
                string sql = "select adv.AdvertisingID,adv.AdvName,adv.AdvPositionID,advPo.AdvName as AdvPositionName,adv.Link,adv.Logo,adv.Note,adv.CreateDate,adv.ExpireDate,adv.Priority,adv.IsVisible from advertising adv,advertisingposition advPo where adv.AdvPositionID=advPo.AdvPositionID";
              
                SqlCommand command = helper.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                   Advertising adv = new Advertising();
                    adv.AdvertisingID = int.Parse(reader["AdvertisingID"].ToString());
                    adv.AdvName = reader["AdvName"].ToString();
                    adv.AdvPositionID = int.Parse(reader["AdvPositionID"].ToString());
                    adv.AdvPositionName = reader["AdvPositionName"].ToString();
                    try
                    {
                        adv.Link = reader["Link"].ToString();
                    }
                    catch
                    {
                        adv.Link = "";
                    }
                    try
                    {
                        adv.Logo = reader["Logo"].ToString();
                    }
                    catch
                    {
                        adv.Logo = "";
                    }
                    try
                    {
                        adv.Note = reader["Note"].ToString();
                    }
                    catch
                    {
                        adv.Note = "";
                    }
                    adv.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
                    adv.ExpireDate = DateTime.Parse(reader["ExpireDate"].ToString());
                    adv.Priority = int.Parse(reader["Priority"].ToString());
                    adv.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    list.Add(adv);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }

        public static List<Advertising> GetListAdvertisingAdminFollowPosition(int advPosition,ConnectionType typeConnect)
        {
            Sqlhelper helper = new Sqlhelper(typeConnect);
            try
            {
                List<Advertising> list = new List<Advertising>();
                string sql = "select adv.AdvertisingID,adv.AdvName,adv.AdvPositionID,advPo.AdvName as AdvPositionName,adv.Link,adv.Logo,adv.Note,adv.CreateDate,adv.ExpireDate,adv.Priority,adv.IsVisible from advertising adv,advertisingposition advPo where adv.AdvPositionID=advPo.AdvPositionID and adv.AdvPositionID=@advPosition";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@advPosition",advPosition);
                SqlCommand command = helper.GetCommand(sql,pa, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Advertising adv = new Advertising();
                    adv.AdvertisingID = int.Parse(reader["AdvertisingID"].ToString());
                    adv.AdvName = reader["AdvName"].ToString();
                    adv.AdvPositionID = int.Parse(reader["AdvPositionID"].ToString());
                    adv.AdvPositionName = reader["AdvPositionName"].ToString();
                    try
                    {
                        adv.Link = reader["Link"].ToString();
                    }
                    catch
                    {
                        adv.Link = "";
                    }
                    try
                    {
                        adv.Logo = reader["Logo"].ToString();
                    }
                    catch
                    {
                        adv.Logo = "";
                    }
                    try
                    {
                        adv.Note = reader["Note"].ToString();
                    }
                    catch
                    {
                        adv.Note = "";
                    }
                    adv.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());
                    adv.ExpireDate = DateTime.Parse(reader["ExpireDate"].ToString());
                    adv.Priority = int.Parse(reader["Priority"].ToString());
                    adv.IsVisible = int.Parse(reader["IsVisible"].ToString());
                    list.Add(adv);
                }
                return list;
            }
            catch (Exception ex)
            {
                Utilitys.WriteLog(ex.Message);
                return null;
            }
            finally
            {
                helper.destroy();
            }
        }
    }
}
